//
//  MobileBindVC.m
//  sensen_ios
//
//  Created by mc on 16/3/24.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "MobileBindVC.h"

#import "GetCaptchaButton.h"

#import "PersonalVC.h"
#import "ModifyTextField.h"

@interface MobileBindVC (){
    ModifyTextField *_mobileField;
    ModifyTextField *_codeField;
    ModifyTextField *_passwordField;
    UIButton *_confirmBtn; //确认按钮
}

@property (nonatomic, strong) GetCaptchaButton *getCaptchButton; // 获取验证码按钮


@end

@implementation MobileBindVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"登录绑定";
    self.view.backgroundColor = [UIColor whiteColor];
    [self createLeftBarButtonItemWithImagename:nil]; // 左侧返回按钮
    [self _initView];
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    // 设置导航栏颜色
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:SS_NAVI_ORANGE_COLOR] forBarMetrics:UIBarMetricsDefault];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.view endEditing:YES];
}



- (void)_initView{
    
    _mobileField = [[ModifyTextField alloc] initWithFrame:CGRectMake(20, 20, SCREEN_WIDTH - 40, 40) placeholder:@"请输入手机号"];
    [self.view addSubview:_mobileField];
    
    _codeField = [[ModifyTextField alloc] initWithFrame:CGRectMake(20, _mobileField.bottom + 20, SCREEN_WIDTH - 40, 40) placeholder:@"请输入验证码"];
    [self.view addSubview:_codeField];
    
    _getCaptchButton = [[GetCaptchaButton alloc] initWithFrame:CGRectMake(0, 0, 80, 30)];
    [self.view addSubview:_getCaptchButton];
    [_getCaptchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_codeField).offset(-10);
        make.centerY.equalTo(_codeField.mas_centerY).offset(0);
        make.size.mas_equalTo(CGSizeMake(80, 30));
    }];
     _getCaptchButton.tag = 100;
     [_getCaptchButton addTarget:self action:@selector(_getCaptcha) forControlEvents:UIControlEventTouchUpInside];
    
    _passwordField = [[ModifyTextField alloc] initWithFrame:CGRectMake(20, _codeField.bottom + 20, SCREEN_WIDTH - 40, 40) placeholder:@"请输入密码"];
    [self.view addSubview:_passwordField];
    
    
    
    
    
    _confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _confirmBtn.frame = CGRectMake(20, _passwordField.bottom + 20, SCREEN_WIDTH - 40, 35);
    [self.view addSubview:_confirmBtn];
    [_confirmBtn setTitle:@"确认绑定" forState:UIControlStateNormal];
    _confirmBtn.layer.cornerRadius = 2;
    [_confirmBtn addTarget:self action:@selector(saveAction) forControlEvents:UIControlEventTouchUpInside];
    [_confirmBtn setBackgroundColor:SS_NAVI_ORANGE_COLOR];
    
    _confirmBtn.layer.borderColor = SS_NAVI_ORANGE_COLOR.CGColor;
    _confirmBtn.layer.borderWidth = 1;
    _confirmBtn.layer.masksToBounds = YES;
    _confirmBtn.layer.cornerRadius = 3.0;

}



#pragma mark - 按钮点击处理
- (void)leftAction:(UIButton *)button {

    // 不绑定，就删除用户信息
    [CommonCache logout];
    [self.navigationController popViewControllerAnimated:YES];

}
// 获取验证码按钮点击
- (void)_getCaptcha {
        if ([_mobileField.text isStringNull]) {
            [SVProgressHUD showOnlyStatus:@"请输入手机号！" withDuration:1];
            return;
        }
        else if (_mobileField.text.length != 11) {
            [SVProgressHUD showOnlyStatus:@"手机号输入不正确！" withDuration:1];
            return;
        }
        [_getCaptchButton getCaptchaWithMobile:_mobileField.text type:CAPTCHA_BIND_PHONE captchaBlock:^(NSString *captcha) {
            _codeField.text = captcha;
        }];
}

// 保存按钮执行事件
- (void)saveAction{
    
    if ([_passwordField.text isStringNull]) {
        [SVProgressHUD showOnlyStatus:@"请输入密码" withDuration:1];
        return;
    }
    
    UserModel *user = [CommonCache getUserInfo];
    NSMutableDictionary *diction = [NSMutableDictionary dictionary];
    [diction setObject:_codeField.text forKey:@"code"];
    [diction setObject:_mobileField.text forKey:@"mobile"];
    [diction setObject:_passwordField.text forKey:@"password"];
    NSLog(@"手机号是 %@", _mobileField.text);
    NSLog(@"验证码是 %@", _codeField.text);
    NSLog(@"密码是 %@", _passwordField.text);
    
    if (!(_mobileField.text && _mobileField.text.length > 0 &&
        _codeField.text && _codeField.text.length > 0 &&_passwordField.text &&_passwordField.text.length > 0)) {
        [SVProgressHUD showErrorWithStatus:@"您未填写完整，请查看" withDuration:3];
        return;
    }
    
    [diction setObject:user.uid forKey:@"uid"];
    
    [SVProgressHUD showWithStatus:nil];
    __weak MobileBindVC * that = self;
    [BoyeHttpClient post:@"BY_User_bind" apiVerson:100 params:diction success:^(AFHTTPRequestOperation *operation, id result) {
        NSLog(@"绑定手机%@", result);
        UserModel *user = [CommonCache getUserInfo];
        user.mobile = _mobileField.text;
        user.password = result;
        [CommonCache setUserInfo:user];
        [SVProgressHUD dismiss];
        [that jumpToIndex];
//        [self.navigationController popViewControllerAnimated:YES];
       
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",error.domain] withDuration:3];
        // 清理本地账户信息
        [CommonCache logout];
//        [self.navigationController popViewControllerAnimated:YES];

        
}];
}

/**
 *
*  跳转到首页
 *  @author  hebiduhebi@126.com
 */
-(void ) jumpToIndex{
//    [self.navigationController popToRootViewControllerAnimated:YES];
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

-(void) setLoginInfo:(UserModel * )user{
    
    [CommonCache setUserInfo:user];
    
    // 先删除模态栈
    [self dismissViewControllerAnimated:YES completion:^{
        self.tabBarController.selectedIndex = 2;

    }];
    
//    self.tabBarController.selectedIndex = 2;

        

}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
